//
//  AuditionVideoViewController.m
//  BaseProject
//
//  Created by yang_0921 on 15/11/3.
//  Copyright © 2015年 Tarena. All rights reserved.
//

#import "AuditionVideoViewController.h"
#import "AuditionSidCell.h"
#import "AuditionVideoViewModel.h"

#import "VedioViewCell.h"


#import "AuditionSidViewController.h"

#import "AuditionDetailViewController.h"

@interface AuditionVideoViewController ()<UICollectionViewDataSource>

@property (nonatomic, strong) AuditionVideoViewModel *videoVM;


@end

@implementation AuditionVideoViewController

implementation_sharedStance(AuditionVideoViewController)

- (AuditionVideoViewModel *)videoVM{
    if (!_videoVM) {
        _videoVM = [AuditionVideoViewModel new];
    }
    return _videoVM;
}

static NSString * const reuseIdentifierSid = @"Cell";
static NSString * const reuseIdentifierVideo = @"VideoCell";


#pragma mark - 设置流水布局 及  cell的大小
- (instancetype)init
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];

    // 设置每一行之间的间距
    layout.minimumLineSpacing = 5;
    layout.minimumInteritemSpacing = 1;
    
    // 设置section之间的间隔
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 5, 0);
    return [self initWithCollectionViewLayout:layout];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
   
    
    // 设置背景的颜色
    self.collectionView.backgroundColor = kRGBColor(226, 225, 225);
    
    // 注册cell
    [self.collectionView registerNib:[UINib nibWithNibName:@"AuditionSidCell" bundle:nil] forCellWithReuseIdentifier:reuseIdentifierSid];
    [self.collectionView registerNib:[UINib nibWithNibName:@"VedioViewCell" bundle:nil] forCellWithReuseIdentifier:reuseIdentifierVideo];
   
    
    // 载入数据
    [self loadData];
    
    // 添加segmented
    [self addSegmented];
    
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[VedioViewCell shareInstance].player pause];
}
#pragma mark - 添加segmented
- (void)addSegmented{
    
    self.title = @"视频";
    [self.collectionView reloadData];
    
}


#pragma mark - 载入数据
- (void)loadData{
    // 头部刷新
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        // 视频
            [self.videoVM refreshDataCompletionHandle:^(NSError *error) {
                [self.collectionView reloadData];
                [self.collectionView.mj_header endRefreshing];
            }];
    }];

    // 注意： 这句必须在头部刷新后实现，否则第一次不会知道调用
    // 调用头部刷新
    [self.collectionView.mj_header beginRefreshing];
    
    
}

#pragma mark <UICollectionViewDataSource
// 设置section个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}
// 每个section中的item数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
   // 视频
        if (section == 0) {
            return [self.videoVM sidRowNumber];
        } else {
            return [self.videoVM rowNumber];
        }
    
}

#pragma mark - cell内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
            /** 是头部的Sid*/
        if (indexPath.section == 0) {
            
            AuditionSidCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifierSid forIndexPath:indexPath];
            
            cell.sidTitleLabel.text = [self.videoVM sidTitleWithRow:indexPath.row];
            [cell.imgsrcBtn setBackgroundImageForState:UIControlStateNormal withURL:[NSURL URLWithString:[self.videoVM sidIconWithRow:indexPath.row]]];
            return cell;
        } else {
    
    
        /** 视频部分    */
        
            VedioViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifierVideo forIndexPath:indexPath];
            
        cell.titleLabel.text = [self.videoVM titleWithRow:indexPath.row];
        cell.desLabel.text = [self.videoVM desWithRow:indexPath.row];
        [cell.durationBT setTitle:[self.videoVM lengthWithRow:indexPath.row] forState:UIControlStateNormal];
        [cell.playCountBT setTitle:[self.videoVM playCountWithRow:indexPath.row] forState:UIControlStateNormal];
        [cell.commentBT setTitle:[self.videoVM replyCountWithRow:indexPath.row] forState:UIControlStateNormal];
        cell.playIV.hidden = NO;
        cell.url = [NSURL URLWithString:[self.videoVM mp4UrlWithRow:indexPath.row]];
        [cell.iconBtn setBackgroundImageForState:UIControlStateNormal withURL:[NSURL URLWithString:[self.videoVM coverWithRow:indexPath.row]]];
        
        return cell;

        }
}



#pragma mark <UICollectionViewDataSource>
// 设置cell大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

    CGFloat width = 0;
   
        if (indexPath.section == 1) {
            width = [UIScreen mainScreen].bounds.size.width;
            return CGSizeMake(width, 300); // 设置固定高度
        } else {
            width = [UIScreen mainScreen].bounds.size.width / self.videoVM.sidRowNumber - 1;
            return CGSizeMake(width, width);
            
        }
}

#pragma mark - 点击cell进入下一个按钮
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
        if (indexPath.section == 0) {   // Sid上的cell
            AuditionSidViewController *sidVC = [[AuditionSidViewController alloc] init];
            sidVC.type = indexPath.row;
            sidVC.title = [self.videoVM sidTitleWithRow:indexPath.row];
            sidVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:sidVC animated:YES];
            return;
        } else {
        
        AuditionDetailViewController *vc = [[AuditionDetailViewController alloc] initWithReplayId:[self.videoVM replayIdForRow:indexPath.row] mp4URL:[NSURL URLWithString:[self.videoVM mp4UrlWithRow:indexPath.row]]];
            vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
        

    }
}


@end
